package com.cskaoyan.wx.controller;

import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.wx.bean.bo.WxAddressDeleteBo;
import com.cskaoyan.wx.bean.bo.WxAddressSaveBo;
import com.cskaoyan.wx.bean.vo.WxAddressDetailVo;
import com.cskaoyan.wx.bean.vo.WxAddressListVo;
import com.cskaoyan.wx.service.WxAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("wx/address")
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;

    /**
     * 功能:地址,/list
     * 作者:whj
     */
    @RequestMapping("list")
    public BaseRespVo list(){

        List<WxAddressListVo> addressListVos = wxAddressService.list();
        return BaseRespVo.ok(addressListVos);
    }


    /**
     * 功能:地址,/detail
     * 作者:whj
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){

        WxAddressDetailVo wxAddressDetailVo = wxAddressService.detail(id);
        return BaseRespVo.ok(wxAddressDetailVo);
    }

    /**
     * 功能:地址,/delete
     * 作者:whj
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody WxAddressDeleteBo wxAddressDeleteBo){
        int id = wxAddressDeleteBo.getId();
        int code = wxAddressService.delete(id);
        if(code == 404){
            return BaseRespVo.error("操作失败");
        }
        return BaseRespVo.ok();
    }

    /**
     * 功能:地址,/save
     * 作者:whj
     */
    @RequestMapping("save")
    public BaseRespVo save(@RequestBody WxAddressSaveBo wxAddressSaveBo){
        int code = wxAddressService.save(wxAddressSaveBo);
        if(code == 404){
            return BaseRespVo.error("操作失败");
        }
        return BaseRespVo.ok(code);
    }
}
